﻿@{
    ViewBag.Title = "会员日志管理";
}
@section scripts
{
    <link href="~/Scripts/miniui/themes/default/large-mode.css" rel="stylesheet" type="text/css" />       
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script>
    <link rel="stylesheet" href="~/Scripts/kindeditor-4.1.10/themes/default/default.css" />
    <script charset="utf-8" src="~/Scripts/kindeditor-4.1.10/kindeditor-all.js"></script>
    <script charset="utf-8" src="~/Scripts/kindeditor-4.1.10/lang/zh_CN.js"></script>    
    <style>
        body {
        font-size:10px;
        padding:0;
        margin:0;
        border:0;
        height:100%;
        overflow:hidden;
        }
        .divnote{ background:#FFF;overflow:auto; }
        .main{ background:#FFF;padding-top:10px;padding-left:20px;}
    </style>
}
    <form id="form1" runat="server" method="post" style="height: 100%">
        <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" id="fresh" name="fresh" iconCls="icon-search" onclick="GetMemberID" plain="true">读卡</a>
                        <span class="separator"></span>
                        <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="NewNote" plain="true">新增</a>
                        <a class="mini-button" id="save" name="save" iconCls="icon-ok" onclick="SaveNote" plain="true">保存</a>
                        <span class="separator"></span>
                        <a class="mini-button" id="close" name="close" iconCls="icon-close" onclick="CloseWindow" plain="true" visible="@ViewBag.close">关闭</a>
                    </td>
                </tr>
            </table> 
        </div> 
        <table style="left:0px;">
                <tr>
                <td width ="66px" align="right">会员卡号： </td>
                <td width ="200px">
                    <input id="FNumber" class="mini-buttonedit"style="width:200px; top: 0px; left: 0px;" 
                     onbuttonclick="GetMemberID" name="FNumber" textName="b" onkeypress="setkeyPress()" align="left"/>
                    </td>
                <td width ="70px" align="right">会员姓名：</td>
                <td width ="122px">
                    <input name="FName" class="mini-textbox" style="width:122px;" enabled="false"/></td>
                <td width ="80px" align="right">联系电话：</td>
                <td width ="200px">
                    <input name="FPhone" class="mini-textbox" style="width:200px;" enabled="false"  maxlength="50" />
                </td>
            </tr>
        </table>
        <div class="mini-fit">
            <div id="tabs1" class="mini-tabs" activeIndex="0" tabPosition="bottom" style="width:100%; height:100%; " plain="false">
                <div id="NoteInput" name="NoteInput" title="服务内容">
                    <table>
                         <tr>
                            <td width ="60px" align="right">内容标题：</td>
                            <td width ="400px" align="left">
                                <input id="FTitle" name="FTitle" class="mini-textbox" style="width:400px;"/>
                            </td>
                            <td width ="80px" align="right">编码：</td>
                            <td width ="600px" align="left">
                                <input id="FNoteNumber" name="FNoteNumber" class="mini-textbox" style="width:200px;" enabled="false"/>
                            </td>
                        </tr>
                    </table>
                    <textarea id="content" name="content" style="width:99%;height:500px;"></textarea>
                    <table style="width:600px;">
                         <tr>
                            <td width ="300px" align="left">模板列表：</td>
                            <td width ="80px" align="center">
                                <a class="mini-button" iconCls="icon-save" onclick="SaveTemplate" plain="true">保存为模板</a>
                            </td>
                             <td width ="80px" align="center">
                                <a class="mini-button" iconCls="icon-remove" onclick="RemoveTemplate" plain="true">删除模板</a>
                            </td>
                             <td width ="80px" align="center">
                                <a class="mini-button" iconCls="icon-expand" onclick="load_template" plain="true">刷新</a>
                            </td>
                        </tr>
                    </table>
                     <div class="mini-fit">   
                        <div id="grid_template" class="mini-datagrid" style="width:100%;height:100%;" pageSize="100"
                                borderStyle="border:1;" url="GetTemplateList" showPager="false" idField="FItemID"
                                allowCellSelect="true" allowCellEdit="true"showColumnsMenu="true"fitColumns="false"  
                                sortMode="client" multiSelect="false" onrowdblclick="GetTemplateInfo"">
                            <div property="columns">
                                <div type="indexcolumn" width="50" headerAlign="center">序号</div>
                                <div field="FTitle" name="FTitle" width="700px" headeralign="left" allowsort="true">内容标题</div>       
                                <div field="FRemark" name="FRemark" width="700px" headeralign="left" allowsort="true"></div>                         
                            </div>
                        </div>
                    </div>
                </div>
                <div id="Storage" name="Storage" title="历史记录" style="height:100%;overflow:hidden;margin-left:20px; margin-top:5px;">
                    <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
                       <table>
                            <tr>
                                <td width ="60px" align="right">日期范围：</td>
                                <td width = "310px;">
                                    @Html.Partial("~/Views/Modular/dxDateRangeEx.cshtml")
                                </td>
                                <td width ="80px" align="center">
                                    <a class="mini-button" iconCls="icon-search" onclick="LoadListData" plain="true">查询</a>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="mini-splitter" style="width:100%;height:92%;">
                        <div size="400" showcollapseButton="true">
                            <div class="mini-fit">   
                                <div id="grid_list" class="mini-datagrid" style="width:100%;height:100%;" pageSize="30"
                                    borderStyle="border:0;"url="GetMemberNoteList" onSelectionChanged="onSelectionChanged">
                                    <div property="columns">
                                        <div field="FDateTime" name="FDateTime" width="150" headeralign="center" allowsort="true" dateFormat="yyyy-MM-dd hh:mm:ss">创建时间</div>
                                        <div field="FTitle" name="FTitle" width="250" headeralign="center" allowsort="true">标题</div>                                
                                    </div>
                                </div>
                            </div>
                        </div>       
                        <div showCollapseButton="true" class="divnote">
                            <div class="main"></div>
                        </div>
                    </div>              
                    
                </div>
            </div>
       </div>
    <input name="KInterID" class="mini-hidden" />
    <input name="FSystemAppID" value="@ViewBag.mSystemAppID" class="mini-hidden" />
    <input name="FMemberID" value="@ViewBag.FMemberID" class="mini-hidden" />
</form>
<script>
    mini.parse();
    var editor2;
    var grid_template = mini.get("grid_template");
    var sSystemAppID = GetQueryString("FSystemAppID");
    var sMemberID = GetQueryString("mMemberID");
    var grid_list = mini.get("grid_list");
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
    if ((sMemberID != "") && (sMemberID != "0")) GetMemberID(sMemberID);

    KindEditor.ready(function (K) {
        editor2 = K.create('textarea[name="content"]', {
            uploadJson: '../asp.net/upload_json.ashx?dirs=proImage&mSystemAppID=' + sSystemAppID + '&thumb=true' + '&iseDitor=true',
            fileManagerJson: '../asp.net/file_manager_json.ashx?dirs=proImage&mSystemAppID=' + sSystemAppID,
            afterBlur: function () { this.sync(); },
            allowFileManager: true
        });
    });
    load_template();
    LoadListData();

    //加载资料列表
    function LoadListData() {
        var sMemberID = mini.getbyName("FMemberID").getValue();
        var cDate1 = mini.getbyName("FDate1").getFormValue();
        var cDate2 = mini.getbyName("FDate2").getFormValue();
        var grid_list = mini.get("grid_list");
        var json = "[{";
        json = json + "'mSystemAppID':" + "'" + sSystemAppID + "',";
        json = json + '"mDate1":' + '"' + cDate1 + '",';
        json = json + '"mDate2":' + '"' + cDate2 + '",';
        json = json + "'mMemberID':" + "'" + sMemberID + "'";
        json = json + "}]";
        grid_list.load({ mydata: json });
    }

    function onSelectionChanged(e) {
        var grid = e.sender;
        var record = grid.getSelected();
        if (record) {
            GetMemberNoteDetail();
        }
    }

    function GetMemberNoteDetail() {        
        var record = grid_list.getSelected();
        var sKinterID = "0";
        if (record) {
            sKinterID = record.KInterID;
            GetMemberNoteDetails(sKinterID);
        }
    }

    function GetMemberNoteDetails(mKinterID) {
        $.ajax({
            url: 'GetMemberNoteDetails',
            data: { mSystemAppID: sSystemAppID, sKInterID: mKinterID },
            dataType: 'text',
            async: false,
            success: function (text) {
                var mdata = mini.decode(text);
                if (mdata.data.length > 0) {
                    var htmlstr = decodeURIComponent(mdata.data[0].FDetails);
                    $(".main").empty();
                    $(".main").append(htmlstr);
                }
            }
        });
    }

    function GetMemberID(FValue) {        
        var FNumber = mini.getbyName("FNumber").getText();
        $.post("/Member/GetMembers", { json: "[{'FItemID':'" + FValue + "','FNumber':'" + FNumber + "', 'mSystemAppID':'" + sSystemAppID + "'}]" }, function (data) {
            var mdata = mini.decode(data);
            if (mdata.data.length > 0) {
                mini.getbyName("FNumber").setValue(mdata.data[0].FItemID);
                mini.getbyName("FNumber").setText(mdata.data[0].FNumber);
                mini.getbyName("FMemberID").setValue(mdata.data[0].FItemID);
                mini.getbyName("FName").setValue(mdata.data[0].FName);
                mini.getbyName("FPhone").setValue(mdata.data[0].FPhone);
                $('#FTitle').focus();
                LoadListData();
            } else {
                mini.getbyName("FNumber").setValue("");
                mini.getbyName("FMemberID").setValue("0");
                mini.getbyName("FName").setValue("");
                mini.getbyName("FPhone").setValue("");
                mini.showTips({ content: "该会员不存在。", state: "success", x: "center", y: "top", timeout: 3000 });
                grid_list.clearRows();
            }            
        });
    }

    function setkeyPress() {
        if (event.keyCode == 13) {
            GetMemberID("0");
        }
    }

    function NewNote() {
        mini.getbyName("FNumber").setText('');
        mini.getbyName("FNumber").setValue('');
        mini.getbyName("FNoteNumber").setValue('');
        mini.getbyName("FName").setValue('');
        mini.getbyName("FPhone").setValue('');
        mini.getbyName("FMemberID").setValue("");
        mini.getbyName("KInterID").setValue("");
        BillState(0);
    }

    function SaveNote() {
        if (mini.getbyName("FNumber").getValue() == '') {
            mini.alert("未选择会员");
            return;
        }
        mini.getbyName("save").setEnabled(false);
        var form = new mini.Form("#form1");
        var html = $("#content").val();
        var sTitle = mini.getbyName("FTitle").getValue();
        var data = form.getData(true, false);
        data.FDetails = encodeURIComponent(html);
        var mjson = mini.encode(data);
        $.post("SaveNote", { json: mjson }, function (data) {
            var ret = mini.decode(data);
            if (ret.status >= 1) {
                mini.getbyName("KInterID").setValue(ret.billid);
                mini.getbyName("FNoteNumber").setValue(ret.billno);
                BillState(1);
                LoadListData();
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {
                mini.alert("异常:" + ret.error);
            }
        });
    }

    function SaveTemplate() {
        if (mini.getbyName("FTitle").getValue() == '') {
            mini.alert("没有输入标题，不能保存模板。");
            return;
        }
        var form = new mini.Form("#form1");
        var html = $("#content").val();
        var sTitle = mini.getbyName("FTitle").getValue();
        var data = form.getData(true, false);
        data.FDetails = encodeURIComponent(html);
        var mjson = mini.encode(data);
        $.post("SaveTemplate", { json: mjson }, function (data) {
            var ret = mini.decode(data);
            if (ret.status >= 1) {
                load_template();
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {
                mini.alert("异常:" + ret.error);
            }
        });
    }

    function load_template() {
        $.ajax({
            url: 'GetTemplateList',
            data: { mSystemAppID: sSystemAppID },
            dataType: 'text',
            async: false,
            success: function (text) {
                var dataR = mini.decode(text);
                grid_template.setData(dataR.data);
            }
        });
    }

    function BillState(flag) {
        switch (flag) {
            case 0:   //新增
                mini.get("FTitle").setEnabled(true);
                mini.get("save").setEnabled(true);
                mini.get("FNumber").setEnabled(true);
                mini.get("fresh").setEnabled(true);
                editor2.readonly(false);
                break;
            case 1:   //保存
                mini.get("FTitle").setEnabled(false);
                mini.get("save").setEnabled(false);
                mini.get("FNumber").setEnabled(false); 
                mini.get("fresh").setEnabled(false);
                editor2.readonly(true);
                break;
        }
    }

    function GetTemplateInfo() {
        var row = grid_template.getSelected();
        if (row) {
            var htmlstr = decodeURIComponent(row.FDetails);
            editor2.html("");
            editor2.insertHtml(htmlstr);
        }
    }

    function RemoveTemplate() {
        var row = grid_template.getSelected();
        if (row) {
            mini.confirm("确定要删除该模板吗？", "提示",
                function (action) {
                    if (action == "ok") {
                        SetRemoveTemplate(row);
                    }
                }
           );
        }
    }

    function SetRemoveTemplate(row) {
        var json = "[{";
        json = json + "'KInterID':'" + row.FTemplateID + "',";
        json = json + "'FDataType':'" + "28" + "',";
        json = json + "'FSystemAppID':'" + sSystemAppID + "'}]";
        $.ajax({
            url: '../Bill/RemoveBillData',
            data: { mdatajson: json },
            type: "post",
            cache: false,
            success: function (txt) {
                var ret = mini.decode(txt);
                if (ret.id != -1) {
                    mini.showMessageBox({
                        showModal: false, width: 250, title: "提示", iconCls: "mini-messagebox-info",
                        message: ret.Status, timeout: 3000, x: "right", y: "bottom"
                    });
                    load_template();
                }
                else {
                    mini.alert("异常:" + ret.Status);
                }
            },
            error: function (xhr) {
                mini.alert("异常:" + xhr.responseText);
            }
        });
    }

    $(function () {
        FormShown();
    });


    function FormShown() {
             
    }
</script>

